#include	"STC32G_SPI.h"

u8 	SPI_RxTimerOut;
u8 	SPI_BUF_type SPI_RxBuffer[SPI_BUF_LENTH];
bit B_SPI_Busy;

void	SPI_Init(SPI_InitTypeDef *SPIx)
{
	if(SPIx->SPI_SSIG == ENABLE)			SSIG = 1; 	//conform Master or Slave by SPI_Mode(ignore SS)
	else									SSIG = 0; 	//conform Master or Slave by SS pin.
	SPI_Start(SPIx->SPI_Enable);
	SPI_FirstBit_Set(SPIx->SPI_FirstBit);
	SPI_Mode_Set(SPIx->SPI_Mode);
	SPI_CPOL_Set(SPIx->SPI_CPOL);
	SPI_CPHA_Set(SPIx->SPI_CPHA);
	SPI_Clock_Select(SPIx->SPI_Speed);
	
	SPI_RxTimerOut = 0;
	B_SPI_Busy = 0;
}

void SPI_SetMode(u8 mode)
{
	if(mode == SPI_Mode_Slave)
	{
		MSTR = 0; 	//��������Ϊ�ӻ�����
		SSIG = 0; 	//SS����ȷ������
	}
	else
	{
		MSTR = 1; 	//ʹ�� SPI ����ģʽ
		SSIG = 1; 	//����SS���Ź���
	}
}

void SPI_WriteByte(u8 dat)		//SPI����һ���ֽ�����
{
	if(ESPI)
	{
		B_SPI_Busy = 1;
		SPDAT = dat;
		while(B_SPI_Busy);  //�ж�ģʽ
	}
	else
	{
		SPDAT = dat;
		while(SPIF == 0); //��ѯģʽ
		SPI_ClearFlag();   //���SPIF��WCOL��־
	}
}

//========================================================================
// ����: void SPI_ReadByte(u8 dat)
// ����: SPI��ѯģʽ��ȡһ���ֽ�����.
// ����: none.
// ����: ��ȡ������.
// �汾: V1.0, 2020-09-14
//========================================================================
u8 SPI_ReadByte(void)
{
	SPDAT = 0xff;
	while(SPIF == 0) ;
	SPI_ClearFlag();   //��0 SPIF��WCOL��־
	return (SPDAT);
}
